//
//  WHNFirstViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//  直播上部四个电台点击推出页

#import "WHNFirstViewController.h"
#import "WHNCollectionViewCell.h"
#import "AFTools.h"
#import "WHNLive.h"
#import <UIImageView+WebCache.h>
#import "MenuControlView.h"
#import "WHNCityList.h"
#import "WHNThirdViewController.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "FitHeader.h"
#import "PlayerButton.h"

@interface WHNFirstViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, MenuControlViewDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)MenuControlView *topView;
@property(nonatomic, strong)NSMutableArray *cityArray;
@property(nonatomic, strong)NSMutableArray *codeArray;
@property(nonatomic, strong)NSString *urlString;
@property(nonatomic, assign)NSInteger pageNum;
@property(nonatomic, assign)NSInteger number;
@property(nonatomic, strong)MBProgressHUD *progressView;

@end

@implementation WHNFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = [NSMutableArray array];
    [self.view NightWithType:UIViewColorTypeNormal];
    self.title = _titleString;
    [self getData];
    [self getCityList];
    [self reloadDataAndRefresh];
    [self.view addSubview:self.progressView];
}
#pragma mark 获取数据
-(void)getData
{
    switch ([_numString integerValue]) {
        case 0:{
            _urlString = [NSString stringWithFormat: @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&radioType=2&device=android&provinceCode=210000&pageSize=15", _pageNum];
            
            break;
        }
        case 1:{
            _urlString = [NSString stringWithFormat: @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&radioType=1&device=android&pageSize=15", _pageNum];
            break;
        }
        case 2:{
            _urlString = [NSString stringWithFormat: @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&radioType=2&device=android&provinceCode=110000&pageSize=15", _pageNum];
            break;
        }
        case 3:{
            _urlString = [NSString stringWithFormat: @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&radioType=3&device=android&pageSize=15", _pageNum];
            break;
        }
        default:
            break;
    }
    [AFTools getDataByURLString:_urlString WithDataBlock:^(id data) {
        NSArray *resultArr = [data objectForKey:@"result"];
        for (NSDictionary *dic in resultArr) {
            WHNLive *live = [[WHNLive alloc]init];
            [live setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:live];
        }
        [_collectionView reloadData];
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}

#pragma mark 获取每个城市对应的数据
-(void)getData:(NSString *)provinceCode
{
    [_dataSource removeAllObjects];
    [AFTools getDataByURLString:[NSString stringWithFormat:@"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&radioType=2&device=android&provinceCode=%@&pageSize=15", _pageNum, provinceCode] WithDataBlock:^(id data) {
        NSArray *resultArr = [data objectForKey:@"result"];
        for (NSDictionary *dic in resultArr) {
            WHNLive *live = [[WHNLive alloc]init];
            [live setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:live];
        }
        [_collectionView reloadData];
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}
#pragma mark 刷新加载
-(void)reloadDataAndRefresh
{
    if ([_numString integerValue] == 2) {
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataSource removeAllObjects];
            _pageNum = 1;
            [self getData:_codeArray[_number]];
            
        }];
        
    }else {
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataSource removeAllObjects];
            _pageNum = 1;
            [self getData];
        }];
        _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _pageNum ++;
            [self getData];
        }];
    }
}
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self.view];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

#pragma mark 获取城市
-(void)getCityList
{
    _cityArray = [NSMutableArray array];
    _codeArray = [NSMutableArray array];
    [AFTools getDataByURLString:@"http://live.ximalaya.com/live-web/v1/getProvinceList?device=android" WithDataBlock:^(id data) {
        for (NSDictionary * dic in [data objectForKey:@"result"]) {
            WHNCityList *list = [[WHNCityList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_cityArray addObject:list.provinceName];
            [_codeArray addObject:list.provinceCode];
        }
        _topView.dataSource = _cityArray;
    }];
}

-(void)loadView
{
    [super loadView];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, 100 * FitHeight);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    if ([_titleString isEqualToString:@"省市台"]) {
        [self.view addSubview:self.topView];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, _topView.frame.origin.y + _topView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 64 - 50 * FitHeight) collectionViewLayout:flowLayout];
    }
    else {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    }
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    [_collectionView NightWithType:UIViewColorTypeNormal];
    
}
//#pragma mark 视图将出现时
//-(void)viewWillAppear:(BOOL)animated
//{
//    self.tabBarController.tabBar.hidden = YES;
//    [PlayerButton shareButton].hidden = YES;
//}
//-(void)viewWillDisappear:(BOOL)animated
//{
//    self.tabBarController.tabBar.hidden = NO;
//    [PlayerButton shareButton].hidden = NO;
//}


#pragma mark 上部滚动初始化
-(MenuControlView *)topView
{
    if (!_topView) {
        _topView = [[MenuControlView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50 * FitHeight)];
        _topView.delegate = self;
    }
    return _topView;
}
#pragma mark - collectionview cell样式
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.kind = WHNCollectionViewKindThreeLabelAndThreePicture;
    if (_dataSource.count != 0) {
        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [_dataSource[indexPath.row] radioCoverSmall]]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        cell.mainImageView.layer.cornerRadius = 5;
        cell.mainImageView.layer.masksToBounds = YES;
        cell.titleLabel.text = [_dataSource[indexPath.row]rname];
        cell.byLabel.text = [NSString stringWithFormat:@"正在直播: %@", [_dataSource[indexPath.row] programName]];
        cell.iconImageView1.image = [UIImage imageNamed:@"sanjiao1"];
        CGFloat a = [[_dataSource[indexPath.row]radioPlayCount]integerValue] / 10000 * 1.0;
        if (a >= 1.0) {
            cell.numLabel.text = [NSString stringWithFormat:@"%.1f万人", a];
        }else {
            cell.numLabel.text = [NSString stringWithFormat:@"%ld人", [[_dataSource[indexPath.row]radioPlayCount]integerValue]];
        }
        cell.iconImageView2.backgroundColor = [UIColor lightGrayColor];
    }
    return cell;
}
#pragma mark item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}
#pragma mark 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WHNThirdViewController *thirdVC = [[WHNThirdViewController alloc] init];
    thirdVC.WHNPic = [_dataSource[indexPath.row]radioCoverSmall];
    thirdVC.WHNCount = [_dataSource[indexPath.row]radioPlayCount];
    thirdVC.RadioId = [_dataSource[indexPath.row]radioId];
    thirdVC.Rname = [_dataSource[indexPath.row]rname];
    [self.navigationController pushViewController:thirdVC animated:YES];
}
#pragma mark - menu代理方法
-(void)didSelectedMenu:(NSInteger)number
{
    _number = number;
    _collectionView.contentOffset = CGPointMake(0, 0);
    [self getData:_codeArray[number]];
}


















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
